#include "s_csam_driver.h"

#include <stdio.h>
#include <string.h>
//#include <stdlib.h>

//#include "s_lock_msg.h"
//#include "s_csam_checksign.h"
//////////////////////////////////////////////////////////////////////////////////
#include "h_cc2640r2f_define.h"
#include "h_cc2640r2f_simpletime.h"
#include "h_cc2640r2f_uart.h"
#include "h_cc2640r2f_gpio.h"

/*
 * If use the RX-TX to one line mode, the rx may get the tx data
 * */
#define S_CSAM_DRIVER_GETDATA_ECHO      					1

#define S_CSAM_DRIVER_09FILE_ENABLE			                        0

//The value shouldn't be less than 30 - because the get sign data length
#define S_CSAM_DRIVER_RXBUFFER_MAXLEN    					CSAM_RX_MAX_LENGTH
#define S_CSAM_DRIVER_TXBUFFER_MAXLEN    					128

#if S_CSAM_DRIVER_09FILE_ENABLE
#define S_CSAM_DRIVER_MALLOC								malloc
#define S_CSAM_DRIVER_FREE 									free
#endif

#define S_CSAM_DRIVER_PRINTHEX								debug_hex
#define S_CSAM_DRIVER_PRINT 								printf



void s_csam_driver_vcc_high(void)
{
    H_CC2640R2F_GPIO_OutputSet(S_PSAMCARD_POWERCONTRL_IO, 0);
}

void s_csam_driver_vcc_low(void)
{
    H_CC2640R2F_GPIO_OutputSet(S_PSAMCARD_POWERCONTRL_IO, 1);
}

static void s_csam_driver_rst_high(void)
{
    H_CC2640R2F_GPIO_OutputSet(S_PSAMCARD_RST_IO, 1);
}

static void s_csam_driver_rst_low(void)
{
    H_CC2640R2F_GPIO_OutputSet(S_PSAMCARD_RST_IO, 0);
}

// static void s_csam_driver_delay_us(unsigned int dly)
// {
// 	delay_us(dly);
// }

static void s_csam_driver_delay_ms(unsigned int dly)
{
    H_CC2640R2F_SimpleTime_Around_Delay(dly);
}

static unsigned int s_csam_driver_getsysms(void)
{
    return H_CC2640R2F_SimpleTime_GetSystickms();
}

static void s_csam_driver_start_rxdata(void)
{
    H_CC2640R2F_UART_RxEnable();
}

static void s_csam_driver_stop_rxdata(void)
{
    H_CC2640R2F_UART_RxDisable();
}

static void s_csam_driver_start_endrxdata(void)
{
	// H_CC2640R2F_UART_RxDisable();
	// H_CC2640R2F_UART_Rx_Data_Release();
}

static unsigned short s_csam_driver_get_onetimerxlen(void)
{
    return H_CC2640R2F_UARTRxBufferLength;
}

static unsigned short s_csam_driver_clear_onetimerxlen(void)
{
    H_CC2640R2F_UARTRxBufferLength = 0U;
	return 0U;
}

static unsigned char *s_csam_driver_get_onetimerxdata(void)
{
    return H_CC2640R2F_UARTRxBuffer;
}

static void s_csam_driver_send_data(const unsigned char *data, \
										unsigned short len)
{
    H_CC2640R2F_UART_TxBuff(data, len);
}
//////////////////////////////////////////////////////////////////////////////////
//05 file
#define S_CSAM_DRIVER_05FILE_TERMINALIDINDEX               	0x1E   //30
#define S_CSAM_DRIVER_05FILE_PASSCODEIDINDEX               	S_CSAM_DRIVER_05FILE_TERMINALIDINDEX - 10
//08 file
#define S_CSAM_DRIVER_08FILE_SERVERCCKSID_INDEX				0
#define S_CSAM_DRIVER_08FILE_LKEYINDEX                     	48
#define S_CSAM_DRIVER_08FILE_PASSKEYPRIVILEGEDTIMEINDEX    	80
#define S_CSAM_DRIVER_08FILE_PASSKEYEFFECTIVELIFEINDEX     	84
//09 file
#define S_CSAM_DRIVER_09FILE_SERVERCCKSID_INDEX				0
#define S_CSAM_DRIVER_09FILE_LOCKID_INDEX					0x30
#define S_CSAM_DRIVER_09FILE_KEYTYPE_INDEX					0x36
#define S_CSAM_DRIVER_09FILE_LOCKBLEMAC_INDEX				0x37
#define S_CSAM_DRIVER_09FILE_STARTTIME_INDEX				0x3D
#define S_CSAM_DRIVER_09FILE_ENDTIME_INDEX					0x41
#define S_CSAM_DRIVER_09FILE_TIMEZONEMSG_INDEX				0x45
#define S_CSAM_DRIVER_09FILE_SERVERSIGNEDDATA_INDEX			0x5A



#define S_CSAM_DRIVER_TIMEOUT                       		(-1)
#define S_CSAM_DRIVER_RSPNOTMATCH                   		(-2)
#define S_CSAM_DRIVER_RSPERROR                      		(-3)
#define S_CSAM_DRIVER_OK                            		0
#define S_CSAM_DRIVER_ERROR                         		(-11)
#define S_CSAM_DRIVER_DATAERROR                     		(-12)

#define S_CSAM_DRIVER_COLDRESETERROR						(-20)
#define S_CSAM_DRIVER_SETAIDERROR							(-21)
#define S_CSAM_DRIVER_GETRSPERROR							(-22)

#define S_CSAM_DRIVER_GETDATADEBUG                 			1

#define S_CSAM_DRIVER_POWERCONTROL_ON               		0
#define S_CSAM_DRIVER_POWERCONTROL_OFF              		1

#define S_CSAM_DRIVER_RSTCONTROL_ENABLE             		0
#define S_CSAM_DRIVER_RSTCONTROL_DISABLE            		1

#define S_CSAM_DRIVER_RSTRSP_TIMEOUT						500//500//ms
#define S_CSAM_DRIVER_CMDRSP_TIMEOUT						2000//ms

#define S_CSAM_DRIVER_RST_TIME                      		100

#define S_CSAM_DRIVER_COLRSTRSP_LEN							12


#define S_CSAM_DRIVER_NORMALCMD_LEN                 		5

#define S_CSAM_DRIVER_05FILE_SIZE                   		36
#define S_CSAM_DRIVER_08FILE_SIZE                   		88
#define S_CSAM_DRIVER_09FILE_SIZE							120



static const unsigned char s_csam_driver_coldrstresp[] = {\
    0x3b, 0xdf, 0x96, 0xff, 0x80, 0x21, 0x45, 0xff, 0x31, 0x07, \
    0xff, 0xff
};

static unsigned char s_csam_driver_rxbuffer[S_CSAM_DRIVER_RXBUFFER_MAXLEN];
static unsigned short s_csam_driver_rxbufferlen = 0U;

static const unsigned char s_csam_driver_getrandom_cmd[S_CSAM_DRIVER_NORMALCMD_LEN] = {0x00, 0x84, 0x00, 0x00, 0x08};
static const unsigned char s_csam_driver_setAIDdata[S_CSAM_DRIVER_NORMALCMD_LEN] = {0x00, 0xA4, 0x00, 0x00, 0x02};
static unsigned char s_csam_driver_getresponsedata[S_CSAM_DRIVER_NORMALCMD_LEN] = {0x00, 0xC0, 0x00, 0x00, 0x00};    //Caution, the last byte is refer to how long you need to get from the system.
static unsigned char s_csam_driver_checksign[S_CSAM_DRIVER_NORMALCMD_LEN] = {0x80, 0xFE, 0x00, 0x00, 0x00};          //Caution, the last byte is refer to how long your data need to check sign.
static const unsigned char s_csam_driver_get05file[S_CSAM_DRIVER_NORMALCMD_LEN] = {0x00, 0xB0, 0x85, 0x00, S_CSAM_DRIVER_05FILE_SIZE};
static const unsigned char s_csam_driver_get08file[S_CSAM_DRIVER_NORMALCMD_LEN] = {0x00, 0xB0, 0x88, 0x00, S_CSAM_DRIVER_08FILE_SIZE};
#if S_CSAM_DRIVER_09FILE_ENABLE
static const unsigned char s_csam_driver_get09file[S_CSAM_DRIVER_NORMALCMD_LEN] = {0x00, 0xB0, 0x89, 0x00, S_CSAM_DRIVER_09FILE_SIZE};
#endif

static unsigned char s_csam_driver_getsign[S_CSAM_DRIVER_NORMALCMD_LEN] = {0x80, 0xFD, 0x00, 0x00, 0x00};			//Caution, the last byte is refer to how long your data need to get sign.

static const unsigned char s_csam_driver_getsign_failed[] = {0x80, 0xFD};

s_csam_driver_init_rslt s_csam_driver_init_rslt_flag = csam_init_failed;
//////////////////////////////////////////////////////////////////////////////////

/*
	minlen for data minlen, if less than minlen it will re-receive data
*/
static signed char s_csam_driver_getdata(unsigned short minilen)
{
	unsigned int timenow = 0U;

	minilen++;

	s_csam_driver_rxbufferlen = 0U;
	s_csam_driver_clear_onetimerxlen();

	s_csam_driver_start_rxdata();

	timenow = s_csam_driver_getsysms();

	//	while(s_csam_driver_rxbufferlen < minilen && \
//		s_csam_driver_getsysms() - \
//			timenow < S_CSAM_DRIVER_CMDRSP_TIMEOUT)
//	{
//		if(0 != s_csam_driver_get_onetimerxlen())
//		{
//
//			memcpy(&s_csam_driver_rxbuffer[ \
//				s_csam_driver_rxbufferlen], \
//					s_csam_driver_get_onetimerxdata(), \
//						s_csam_driver_get_onetimerxlen());
//			s_csam_driver_rxbufferlen += \
//				s_csam_driver_get_onetimerxlen();
//
//			if(s_csam_driver_rxbufferlen < minilen)
//			{
//				s_csam_driver_clear_onetimerxlen();
//				s_csam_driver_start_rxdata();
//			}
//			else
//			{
//				break;
//			}
//
//		}
//	}


    while((s_csam_driver_rxbufferlen < minilen) && \
        (s_csam_driver_getsysms() - \
            timenow < S_CSAM_DRIVER_CMDRSP_TIMEOUT))
    {
        if(0 != s_csam_driver_get_onetimerxlen())
        {
//            printf("len %d\r\n", s_csam_driver_rxbufferlen);
            memcpy(&s_csam_driver_rxbuffer[ \
                s_csam_driver_rxbufferlen], \
                    s_csam_driver_get_onetimerxdata(), \
                        s_csam_driver_get_onetimerxlen());
            s_csam_driver_rxbufferlen += \
                s_csam_driver_get_onetimerxlen();


            s_csam_driver_clear_onetimerxlen();
            s_csam_driver_start_rxdata();

//            printf("new len %d\r\n", s_csam_driver_rxbufferlen);
        }
    }


	s_csam_driver_start_endrxdata();

	if(s_csam_driver_rxbufferlen < minilen)
	{
#if !LOCK_BOARD_RELEASE
		printf("s_csam_driver_rxbufferlen < minilen\r\n");
#endif
		return S_CSAM_DRIVER_DATAERROR;
	}
	else
	{
//	    printf("need is %d, real is %d\r\n", minilen, s_csam_driver_rxbufferlen);
//	    S_CSAM_DRIVER_PRINTHEX(s_csam_driver_rxbuffer, s_csam_driver_rxbufferlen);
		// printf("--rx_buffer %d\r\n", s_csam_driver_rxbufferlen);
		// debug_hex(s_csam_driver_rxbuffer, S_CSAM_DRIVER_RXBUFFER_MAXLEN);
		return S_CSAM_DRIVER_OK;
	}
}

static signed char s_csam_driver_senddata(const unsigned char *data, \
											unsigned short length)
{
	if(NULL == data || length > S_CSAM_DRIVER_TXBUFFER_MAXLEN)
	{
		return S_CSAM_DRIVER_DATAERROR;
	}
	s_csam_driver_send_data(data, length);
	return S_CSAM_DRIVER_OK;
}

static signed char s_csam_driver_getColdResetRsp(void)
{
//	unsigned char i = 0U;
	signed char rslt = S_CSAM_DRIVER_OK;

	rslt = s_csam_driver_getdata(S_CSAM_DRIVER_COLRSTRSP_LEN);

	if(S_CSAM_DRIVER_OK == rslt)
	{
		if(memcmp(s_csam_driver_rxbuffer, \
					s_csam_driver_coldrstresp, \
						S_CSAM_DRIVER_COLRSTRSP_LEN))
		{
			// S_CSAM_DRIVER_PRINTHEX(s_csam_driver_rxbuffer,
			// 			S_CSAM_DRIVER_COLRSTRSP_LEN);
			rslt = S_CSAM_DRIVER_RSPNOTMATCH;
		}
	}

	// s_csam_driver_start_endrxdata();

	return rslt;
}



static signed char s_csam_driver_setAID(unsigned char *AID, \
										unsigned char *rsp)
{
	signed char rslt = S_CSAM_DRIVER_OK;

	s_csam_driver_senddata(s_csam_driver_setAIDdata, \
							S_CSAM_DRIVER_NORMALCMD_LEN);

#if S_CSAM_DRIVER_GETDATA_ECHO
	rslt = s_csam_driver_getdata(S_CSAM_DRIVER_NORMALCMD_LEN);
#else
	rslt = s_csam_driver_getdata(0);
#endif


	if(S_CSAM_DRIVER_OK == rslt)
	{
		//Check cmd rsp whether match
#if S_CSAM_DRIVER_GETDATA_ECHO
	    if(s_csam_driver_rxbuffer[\
              S_CSAM_DRIVER_NORMALCMD_LEN] == \
                s_csam_driver_setAIDdata[1])
#else
		if(s_csam_driver_rxbuffer[0] == \
				s_csam_driver_setAIDdata[1])
#endif
		{
			//Send the AID command
			s_csam_driver_senddata(AID, 2);

#if S_CSAM_DRIVER_GETDATA_ECHO
			rslt = s_csam_driver_getdata(2);
#else
			rslt = s_csam_driver_getdata(1);
#endif
			if(S_CSAM_DRIVER_OK == rslt)
			{
				//The first two data msust be AID[0:1], 
				//but we didnt need them
#if S_CSAM_DRIVER_GETDATA_ECHO
			    rsp[0] = s_csam_driver_rxbuffer[2];
                rsp[1] = s_csam_driver_rxbuffer[3];
#else
				rsp[0] = s_csam_driver_rxbuffer[0];
				rsp[1] = s_csam_driver_rxbuffer[1];
#endif
			}
		}
		else
		{
#if !LOCK_BOARD_RELEASE
			 S_CSAM_DRIVER_PRINT("AID the rx is 0x%02x, and real is 0x%02x\r\n",
				s_csam_driver_rxbuffer[0],
				s_csam_driver_setAIDdata[1]);
#endif
			rslt = S_CSAM_DRIVER_RSPNOTMATCH;
		}
	}

	// s_csam_driver_start_endrxdata();

	return rslt;
}

static signed char S_CSAM_Driver_GetRsp(unsigned char *rsp, \
											unsigned char length)
{
	signed char rslt = S_CSAM_DRIVER_OK;
	s_csam_driver_getresponsedata[S_CSAM_DRIVER_NORMALCMD_LEN - 1] = length;
	s_csam_driver_senddata(s_csam_driver_getresponsedata, \
							S_CSAM_DRIVER_NORMALCMD_LEN);

	rslt = s_csam_driver_getdata(S_CSAM_DRIVER_NORMALCMD_LEN);

	if(S_CSAM_DRIVER_OK == rslt)
	{
		//Check the rsp whether match
#if S_CSAM_DRIVER_GETDATA_ECHO
	    if(s_csam_driver_rxbuffer[S_CSAM_DRIVER_NORMALCMD_LEN] \
	            == s_csam_driver_getresponsedata[1])
#else
		if(s_csam_driver_rxbuffer[0] == s_csam_driver_getresponsedata[1])
#endif
		{
			//Check rsp
#if S_CSAM_DRIVER_GETDATA_ECHO
		    if(s_csam_driver_rxbuffer[\
                  S_CSAM_DRIVER_NORMALCMD_LEN + 1 + length] \
		            == 0x90 && \
                s_csam_driver_rxbuffer[\
                   S_CSAM_DRIVER_NORMALCMD_LEN + 2 + length] \
                   == 0x00)
            {
                memcpy(rsp, &s_csam_driver_rxbuffer[\
                    S_CSAM_DRIVER_NORMALCMD_LEN + 1], length);
            }
#else
			if(s_csam_driver_rxbuffer[1 + length] == 0x90 && \
				s_csam_driver_rxbuffer[2 + length] == 0x00)
			{
				memcpy(rsp, &s_csam_driver_rxbuffer[1], length);
			}
#endif
			else
			{
				rslt = S_CSAM_DRIVER_RSPERROR;
			}
		}
		else
		{
			rslt = S_CSAM_DRIVER_RSPERROR;
		}
	}

	// s_csam_driver_start_endrxdata();
	
	return rslt;

}

static signed char S_CSAM_Driver_Get05File(unsigned char *data, \
											unsigned short length)
{
	signed char rslt = S_CSAM_DRIVER_OK;

	s_csam_driver_senddata(s_csam_driver_get05file, \
								S_CSAM_DRIVER_NORMALCMD_LEN);

	rslt = s_csam_driver_getdata(S_CSAM_DRIVER_NORMALCMD_LEN);

	if(S_CSAM_DRIVER_OK == rslt)
	{
		//Check cmd rsp whether match
#if S_CSAM_DRIVER_GETDATA_ECHO
	    if(s_csam_driver_rxbuffer[S_CSAM_DRIVER_NORMALCMD_LEN] \
	            == s_csam_driver_get05file[1])
        {
            //Check rsp
            if(s_csam_driver_rxbuffer[\
                  S_CSAM_DRIVER_NORMALCMD_LEN + 1 + length] \
                    == 0x90 && \
                s_csam_driver_rxbuffer[\
                   S_CSAM_DRIVER_NORMALCMD_LEN + 2 + length] \
                       == 0x00)
            {
                memcpy(data, &s_csam_driver_rxbuffer[\
                         S_CSAM_DRIVER_NORMALCMD_LEN + 1], length);
            }
#else
		if(s_csam_driver_rxbuffer[0] == s_csam_driver_get05file[1])
		{
			//Check rsp
			if(s_csam_driver_rxbuffer[1 + length] == 0x90 && \
				s_csam_driver_rxbuffer[2 + length] == 0x00)
			{
				memcpy(data, &s_csam_driver_rxbuffer[1], length);
			}
#endif
			else
			{
			    // printf("05 S_CSAM_DRIVER_RSPERROR, 0x%02x, 0x%02x\r\n", s_csam_driver_rxbuffer[S_CSAM_DRIVER_NORMALCMD_LEN + 1 + length],
			    //        s_csam_driver_rxbuffer[S_CSAM_DRIVER_NORMALCMD_LEN + 2 + length]);
				rslt = S_CSAM_DRIVER_RSPERROR;
			}
		}
		else
		{
//		    printf("05 S_CSAM_DRIVER_RSPNOTMATCH\r\n");
		    rslt = S_CSAM_DRIVER_RSPNOTMATCH;
		}
	}

	// s_csam_driver_start_endrxdata();

	return rslt;
}


static signed char S_CSAM_Driver_Get08File(unsigned char *data, \
											unsigned short length)
{
	signed char rslt = S_CSAM_DRIVER_OK;

	s_csam_driver_senddata(s_csam_driver_get08file, \
								S_CSAM_DRIVER_NORMALCMD_LEN);

	rslt = s_csam_driver_getdata(S_CSAM_DRIVER_NORMALCMD_LEN);

	if(S_CSAM_DRIVER_OK == rslt)
	{
		//Check cmd rsp whether match
#if S_CSAM_DRIVER_GETDATA_ECHO
	    if(s_csam_driver_rxbuffer[S_CSAM_DRIVER_NORMALCMD_LEN] \
	            == s_csam_driver_get08file[1])
        {
            //Check rsp
            if(s_csam_driver_rxbuffer[\
                  S_CSAM_DRIVER_NORMALCMD_LEN + 1 + length] \
                    == 0x90 && \
                s_csam_driver_rxbuffer[\
                   S_CSAM_DRIVER_NORMALCMD_LEN + 2 + length] \
                   == 0x00)
            {
                memcpy(data, &s_csam_driver_rxbuffer[\
                 S_CSAM_DRIVER_NORMALCMD_LEN + 1], length);
            }
#else
		if(s_csam_driver_rxbuffer[0] == s_csam_driver_get08file[1])
		{
			//Check rsp
			if(s_csam_driver_rxbuffer[1 + length] == 0x90 && \
				s_csam_driver_rxbuffer[2 + length] == 0x00)
			{
				memcpy(data, &s_csam_driver_rxbuffer[1], length);
			}
#endif
			else
			{
				// S_CSAM_DRIVER_PRINT("08 file rsp errpr\r\n");
				rslt = S_CSAM_DRIVER_RSPERROR;
			}
		}
		else
		{
			// S_CSAM_DRIVER_PRINT("08 file rsp not match\r\n");
			rslt = S_CSAM_DRIVER_RSPNOTMATCH;
		}
	}
	else
	{
		// S_CSAM_DRIVER_PRINT("08 file get data out error\r\n");
	}

	// s_csam_driver_start_endrxdata();

	return rslt;
}

#if S_CSAM_DRIVER_09FILE_ENABLE
static signed char S_CSAM_Driver_Get09File(unsigned char *data, \
											unsigned short length)
{
	signed char rslt = S_CSAM_DRIVER_OK;

	s_csam_driver_senddata(s_csam_driver_get09file, \
								S_CSAM_DRIVER_NORMALCMD_LEN);

	rslt = s_csam_driver_getdata(S_CSAM_DRIVER_NORMALCMD_LEN);

	if(S_CSAM_DRIVER_OK == rslt)
	{
		//Check cmd rsp whether match
#if S_CSAM_DRIVER_GETDATA_ECHO
	    if(s_csam_driver_rxbuffer[S_CSAM_DRIVER_NORMALCMD_LEN] \
	            == s_csam_driver_get09file[1])
        {
            //Check rsp
            if(s_csam_driver_rxbuffer[\
                  S_CSAM_DRIVER_NORMALCMD_LEN + 1 + length] \
                    == 0x90 && \
                s_csam_driver_rxbuffer[\
                   S_CSAM_DRIVER_NORMALCMD_LEN + 2 + length] \
                   == 0x00)
            {
                memcpy(data, &s_csam_driver_rxbuffer[\
                 S_CSAM_DRIVER_NORMALCMD_LEN + 1], length);
            }
#else
		if(s_csam_driver_rxbuffer[0] == s_csam_driver_get09file[1])
		{
			//Check rsp
			if(s_csam_driver_rxbuffer[1 + length] == 0x90 && \
				s_csam_driver_rxbuffer[2 + length] == 0x00)
			{
				memcpy(data, &s_csam_driver_rxbuffer[1], length);
			}
#endif
			else
			{
				// S_CSAM_DRIVER_PRINT("08 file rsp errpr\r\n");
				rslt = S_CSAM_DRIVER_RSPERROR;
			}
		}
		else
		{
			// S_CSAM_DRIVER_PRINT("08 file rsp not match\r\n");
			rslt = S_CSAM_DRIVER_RSPNOTMATCH;
		}
	}
	else
	{
		// S_CSAM_DRIVER_PRINT("08 file get data out error\r\n");
	}

	// s_csam_driver_start_endrxdata();

	return rslt;
}
#endif

signed char S_CSAM_Driver_GetRandomNums(unsigned char *random)
{
	signed char rslt = S_CSAM_DRIVER_OK;

	s_csam_driver_senddata(s_csam_driver_getrandom_cmd, \
							S_CSAM_DRIVER_NORMALCMD_LEN);

#if S_CSAM_DRIVER_GETDATA_ECHO
	rslt = s_csam_driver_getdata(S_CSAM_DRIVER_NORMALCMD_LEN);
#else
	rslt = s_csam_driver_getdata(0);
#endif

	if(S_CSAM_DRIVER_OK == rslt)
	{
		//Check cmd rsp whether match
#if S_CSAM_DRIVER_GETDATA_ECHO
		// printf("--rx len is %d\r\n", s_csam_driver_rxbufferlen);
	    // if(s_csam_driver_rxbuffer[S_CSAM_DRIVER_NORMALCMD_LEN] \
	    //         == s_csam_driver_getrandom_cmd[1])
	    // {
     //        //Check the last two data
     //        if(s_csam_driver_rxbuffer[\
     //            S_CSAM_DRIVER_RANDOMDATA_LEN + \
     //            S_CSAM_DRIVER_NORMALCMD_LEN - 1] == \
     //                0x90 && \
     //                s_csam_driver_rxbuffer[\
     //                    S_CSAM_DRIVER_RANDOMDATA_LEN + \
     //                    S_CSAM_DRIVER_NORMALCMD_LEN] == \
     //                        0x00)
     //        {
     //            memcpy(random, \
     //                   &s_csam_driver_rxbuffer[\
     //                   S_CSAM_DRIVER_NORMALCMD_LEN + 1], \
     //                    S_CSAM_DRIVER_RANDOMDATA_LEN);
     //        }

		if(s_csam_driver_rxbuffer[\
			S_CSAM_DRIVER_RANDOMDATA_LEN + \
			S_CSAM_DRIVER_NORMALCMD_LEN - 1] == \
			0x90 && \
			s_csam_driver_rxbuffer[\
			S_CSAM_DRIVER_RANDOMDATA_LEN + \
			S_CSAM_DRIVER_NORMALCMD_LEN] == \
			0x00)
		{
			memcpy(random, \
					&s_csam_driver_rxbuffer[\
					S_CSAM_DRIVER_NORMALCMD_LEN + 1], \
					S_CSAM_DRIVER_RANDOMDATA_LEN);
		}
		else if(s_csam_driver_rxbuffer[\
				S_CSAM_DRIVER_RANDOMDATA_LEN - 1] == \
					0x90 && \
					s_csam_driver_rxbuffer[\
						S_CSAM_DRIVER_RANDOMDATA_LEN] == \
							0x00)
		{
			memcpy(random, &s_csam_driver_rxbuffer[1], \
					S_CSAM_DRIVER_RANDOMDATA_LEN);
		}
		else
		{
			rslt = S_CSAM_DRIVER_RSPERROR;
		}
#else
		if(s_csam_driver_rxbuffer[0] == s_csam_driver_getrandom_cmd[1])
		{
			//Check the last two data
			if(s_csam_driver_rxbuffer[\
				S_CSAM_DRIVER_RANDOMDATA_LEN - 1] == \
					0x90 && \
					s_csam_driver_rxbuffer[\
						S_CSAM_DRIVER_RANDOMDATA_LEN] == \
							0x00)
			{
				memcpy(random, &s_csam_driver_rxbuffer[1], \
						S_CSAM_DRIVER_RANDOMDATA_LEN);
			}

			else
			{
				rslt = S_CSAM_DRIVER_RSPERROR;
			}
		}
		else
		{
			// printf("Get random S_CSAM_DRIVER_RSPNOTMATCH, it is 0x%02x and should be 0x%02x\r\n", \
			// 	s_csam_driver_rxbuffer[S_CSAM_DRIVER_NORMALCMD_LEN], \
			// 	s_csam_driver_getrandom_cmd[1]);
			debug_hex(s_csam_driver_rxbuffer, s_csam_driver_rxbufferlen);
			rslt = S_CSAM_DRIVER_RSPNOTMATCH;
		}
#endif
	}


	
	// s_csam_driver_start_endrxdata();
	return rslt;
}

signed char S_CSAM_Driver_CheckSign(unsigned char *signdata, \
							unsigned char *rsp)
{
	signed char rslt = S_CSAM_DRIVER_OK;

	s_csam_driver_checksign[S_CSAM_DRIVER_NORMALCMD_LEN - 1] = signdata[0];

//	printf("--Check sign send data len %d\r\n", s_csam_driver_checksign[S_CSAM_DRIVER_NORMALCMD_LEN - 1]);
	s_csam_driver_senddata(s_csam_driver_checksign, \
							S_CSAM_DRIVER_NORMALCMD_LEN);
//	printf("--Check sign send finished\r\n");

#if S_CSAM_DRIVER_GETDATA_ECHO
	rslt = s_csam_driver_getdata(S_CSAM_DRIVER_NORMALCMD_LEN);
#else
	rslt = s_csam_driver_getdata(0);
#endif

//	printf("--Check sign get data\r\n");
	if(S_CSAM_DRIVER_OK == rslt)
	{
		//Check cmd rsp whether match
#if S_CSAM_DRIVER_GETDATA_ECHO
	    if(s_csam_driver_rxbuffer[S_CSAM_DRIVER_NORMALCMD_LEN] \
	            == s_csam_driver_checksign[1])
        {
            s_csam_driver_senddata(&signdata[1], signdata[0]);

//            printf("--check sign send data finished\r\n");

            rslt = s_csam_driver_getdata(signdata[0]);

//            printf("--check sign get data finished\r\n");

            if(S_CSAM_DRIVER_OK == rslt)
            {
                if(s_csam_driver_rxbuffer[signdata[0]] == 0x90 && \
                    s_csam_driver_rxbuffer[signdata[0] + 1] == 0x00)
                {
//                     printf("--Check sign success\r\n");
                }
                else
                {
//                    printf("--Check sign rsp error\r\n");
                    rslt = S_CSAM_DRIVER_RSPERROR;
                }
                //Copy the rsp data
                memcpy(rsp, &s_csam_driver_rxbuffer[signdata[0]], 2);
            }
#else
		if(s_csam_driver_rxbuffer[0] == s_csam_driver_checksign[1])
		{
			s_csam_driver_senddata(&signdata[1], signdata[0]);

			rslt = s_csam_driver_getdata(1);

			if(S_CSAM_DRIVER_OK == rslt)
			{
				if(s_csam_driver_rxbuffer[0] == 0x90 && \
					s_csam_driver_rxbuffer[1] == 0x00)
				{
					// printf("Check sign success\r\n");
				}
				else
				{

					rslt = S_CSAM_DRIVER_RSPERROR;
				}
				//Copy the rsp data
				memcpy(rsp, s_csam_driver_rxbuffer, 2);
			}
#endif

		}
		else
		{
//		    printf("--check sign rsp not match\r\n");
			rslt = S_CSAM_DRIVER_RSPNOTMATCH;
		}
	}
	else
	{
		 printf("Signed out bad\r\n");
	}

	// s_csam_driver_start_endrxdata();

	return rslt;
}

signed char S_CSAM_Driver_GetSign(unsigned char *data2sign, \
									unsigned short datalength, \
									unsigned char *datasigned, \
									unsigned char *rsp)
{
	signed char rslt = S_CSAM_DRIVER_OK;

	s_csam_driver_getsign[S_CSAM_DRIVER_NORMALCMD_LEN - 1] = datalength;

	s_csam_driver_senddata(s_csam_driver_getsign, \
							S_CSAM_DRIVER_NORMALCMD_LEN);

#if S_CSAM_DRIVER_GETDATA_ECHO
	rslt = s_csam_driver_getdata(S_CSAM_DRIVER_NORMALCMD_LEN);
#else
	rslt = s_csam_driver_getdata(0);
#endif

	if(S_CSAM_DRIVER_OK == rslt)
	{
//		printf("Get sign command success\r\n");
		// Check cmd rsp whether match
#if S_CSAM_DRIVER_GETDATA_ECHO
	    if(s_csam_driver_rxbuffer[S_CSAM_DRIVER_NORMALCMD_LEN] \
	            == s_csam_driver_getsign[1])
        {
            s_csam_driver_senddata(data2sign, datalength);

            rslt = s_csam_driver_getdata(datalength);

            if(S_CSAM_DRIVER_OK == rslt)
            {
                if(s_csam_driver_rxbuffer[datalength] == 0x61)
                {
                    // printf("Get sign success\r\n");
                    if(S_CSAM_DRIVER_OK == S_CSAM_Driver_GetRsp(datasigned, s_csam_driver_rxbuffer[datalength + 1]))
                    {
//                        printf("Get sign - rsp success\r\n");
                        rsp[0] = 0x90;
                        rsp[1] = 0x00;
                    }
                    else
                    {
//                        printf("Get sign - rsp failed\n");
                        memcpy(rsp, s_csam_driver_getsign_failed, 2);
                        rslt = S_CSAM_DRIVER_RSPNOTMATCH;
                    }
                }
                else
                {
                    rslt = S_CSAM_DRIVER_RSPERROR;
                }
            }
#else
		if(s_csam_driver_rxbuffer[0] == s_csam_driver_getsign[1])
		{
			s_csam_driver_senddata(data2sign, datalength);

			rslt = s_csam_driver_getdata(1);

			if(S_CSAM_DRIVER_OK == rslt)
			{
				if(s_csam_driver_rxbuffer[0] == 0x61)
				{
					// printf("Get sign success\r\n");
					if(S_CSAM_DRIVER_OK == S_CSAM_Driver_GetRsp(datasigned, s_csam_driver_rxbuffer[1]))
					{
						printf("Get sign - rsp success\r\n");
						rsp[0] = 0x90;
						rsp[1] = 0x00;
					}
					else
					{
						printf("Get sign - rsp failed\n");
						memcpy(rsp, s_csam_driver_getsign_failed, 2);
						rslt = S_CSAM_DRIVER_RSPNOTMATCH;
					}
				}
				else
				{
					printf("Get sign failed\r\n");
					//Copy the rsp data
					memcpy(rsp, s_csam_driver_rxbuffer, 2);
					rslt = S_CSAM_DRIVER_RSPERROR;
				}

			}
#endif

		}
		else
		{
			// printf("Get sign S_CSAM_DRIVER_RSPNOTMATCH\r\n");
			rslt = S_CSAM_DRIVER_RSPNOTMATCH;
		}
	}
	// else
	// {
	// 	S_CSAM_DRIVER_PRINT("Get signed bad\r\n");
	// }

	// s_csam_driver_start_endrxdata();

	return rslt;
}

signed char S_CSAM_Driver_Get05FilePara(unsigned char *data, \
											unsigned char type)
{
	signed char rslt = 0;
	unsigned char file05data[S_CSAM_DRIVER_05FILE_SIZE];

	if(NULL == data)
	{
		return S_CSAM_DRIVER_DATAERROR;
	}

	rslt = S_CSAM_Driver_Get05File(file05data, S_CSAM_DRIVER_05FILE_SIZE);

	if(rslt == 0)
	{
		switch(type)
		{
			case S_CSAM_DRIVER_05FILE_TERMINALID_TYPE:
				memcpy(data, &file05data[S_CSAM_DRIVER_05FILE_TERMINALIDINDEX], \
						S_CSAM_DRIVER_05FILE_TERMINALID_SIZE);
				break;

			case S_CSAM_DRIVER_05FILE_PASSCODEID_TYPE:
				memcpy(data, &file05data[S_CSAM_DRIVER_05FILE_PASSCODEIDINDEX], \
						S_CSAM_DRIVER_05FILE_PASSCODEID_SIZE);
				break;

			default:
				break;

		}
	}

	return rslt;
}



signed char S_CSAM_Driver_Get08FilePara(unsigned char *data, \
											unsigned char type)
{
	signed char rslt = 0;
	unsigned char file08data[S_CSAM_DRIVER_08FILE_SIZE];
	const char *file08data_serverCCKSID = NULL;

	if(NULL == data)
	{
		return S_CSAM_DRIVER_DATAERROR;
	}

	rslt = S_CSAM_Driver_Get08File(file08data, S_CSAM_DRIVER_08FILE_SIZE);

	if(rslt == 0)
	{
		switch(type)
		{
			case S_CSAM_DRIVER_08FILE_SERVERCCKSID_TYPE:
				file08data_serverCCKSID = (char *)\
					&file08data[\
					S_CSAM_DRIVER_08FILE_SERVERCCKSID_INDEX];

				data[0] = (unsigned char )strlen(file08data_serverCCKSID);
				memcpy(&data[1], &file08data[S_CSAM_DRIVER_08FILE_SERVERCCKSID_INDEX], \
						data[0]);
				break;
			case S_CSAM_DRIVER_08FILE_LKEY_TYPE:
				memcpy(data, &file08data[S_CSAM_DRIVER_08FILE_LKEYINDEX], \
						S_CSAM_DRIVER_08FILE_LKEY_SIZE);
				break;

			case S_CSAM_DRIVER_08FILE_PASSKEYPRIVILEGEDTIME_TYPE:
				memcpy(data, &file08data[S_CSAM_DRIVER_08FILE_PASSKEYPRIVILEGEDTIMEINDEX], \
						S_CSAM_DRIVER_08FILE_PASSKEYPRIVILEGEDTIME_SIZE);
				break;

			case S_CSAM_DRIVER_08FILE_PASSKEYEFFECTIVELIFE_TYPE:
				memcpy(data, &file08data[S_CSAM_DRIVER_08FILE_PASSKEYEFFECTIVELIFEINDEX], \
						S_CSAM_DRIVER_08FILE_PASSKEYEFFECTIVELIFE_SIZE);

			default:
				break;

		}
	}

	return rslt;
}

#if S_CSAM_DRIVER_09FILE_ENABLE
p_s_csam_driver_09file_t S_CSAM_Driver_Get09File_StructPoint(void)
{
	const char *s_csam_09_file_data_server = NULL;
	unsigned char *s_csam_09_file_data = NULL;


	s_csam_driver_09file_t *data_09file_t = (s_csam_driver_09file_t *)S_CSAM_DRIVER_MALLOC(sizeof(s_csam_driver_09file_t));
	if(NULL == data_09file_t)
	{
		return NULL;
	}

	s_csam_09_file_data = (unsigned char *)S_CSAM_DRIVER_MALLOC(\
		sizeof(unsigned char ) * S_CSAM_DRIVER_09FILE_SIZE);
	if(NULL == s_csam_09_file_data)
	{
		S_CSAM_DRIVER_FREE(data_09file_t);
		data_09file_t = NULL;
		return NULL;
	}

	if(S_CSAM_DRIVER_OK != S_CSAM_Driver_Get09File(\
		s_csam_09_file_data, S_CSAM_DRIVER_09FILE_SIZE))
	{
		S_CSAM_DRIVER_FREE(data_09file_t);
		data_09file_t = NULL;
		S_CSAM_DRIVER_FREE(s_csam_09_file_data);
		s_csam_09_file_data = NULL;

		return NULL;
	}

	s_csam_09_file_data_server = (char *)\
			&s_csam_09_file_data[\
			S_CSAM_DRIVER_09FILE_SERVERCCKSID_INDEX];
	// printf("The 09 data is %d\r\n");
	// debug_hex(s_csam_09_file_data, S_CSAM_DRIVER_09FILE_SIZE);

	// Server CCKSID
	data_09file_t->serverCCKSID_len = (unsigned char)strlen(\
		s_csam_09_file_data_server);
	data_09file_t->serverCCKSID = \
		(unsigned char *)S_CSAM_DRIVER_MALLOC(\
			sizeof(unsigned char) * (\
				data_09file_t->serverCCKSID_len));
	if(NULL == data_09file_t->serverCCKSID)
	{
		S_CSAM_DRIVER_FREE(data_09file_t);
		data_09file_t = NULL;
		S_CSAM_DRIVER_FREE(s_csam_09_file_data);
		s_csam_09_file_data = NULL;

		return NULL;
	}
	memcpy(data_09file_t->serverCCKSID, \
			&s_csam_09_file_data[\
				S_CSAM_DRIVER_09FILE_SERVERCCKSID_INDEX], \
			data_09file_t->serverCCKSID_len);

	// Lock ID
	memcpy(data_09file_t->lockID, \
			&s_csam_09_file_data[\
				S_CSAM_DRIVER_09FILE_LOCKID_INDEX], \
			S_CSAM_DRIVER_09FILE_LOCKID_SIZE);
	// Key type
	data_09file_t->keytype = s_csam_09_file_data[S_CSAM_DRIVER_09FILE_KEYTYPE_INDEX];
	// BLE MAC
	memcpy(data_09file_t->lockBLEMAC, \
			&s_csam_09_file_data[\
				S_CSAM_DRIVER_09FILE_LOCKBLEMAC_INDEX], \
			S_CSAM_DRIVER_09FILE_LOCKBLEMAC_SIZE);
	// Start time
	memcpy(data_09file_t->starttime, \
			&s_csam_09_file_data[\
				S_CSAM_DRIVER_09FILE_STARTTIME_INDEX], \
			S_CSAM_DRIVER_09FILE_UTCTIME_SIZE);
	// End time
	memcpy(data_09file_t->endtime, \
			&s_csam_09_file_data[\
				S_CSAM_DRIVER_09FILE_ENDTIME_INDEX], \
			S_CSAM_DRIVER_09FILE_UTCTIME_SIZE);
	// Time zone msg
	memcpy(data_09file_t->timezone_msg, \
			&s_csam_09_file_data[\
				S_CSAM_DRIVER_09FILE_TIMEZONEMSG_INDEX], \
			S_CSAM_DRIVER_09FILE_TIMEZONEMSG_SIZE);
	// Server signed data
	memcpy(data_09file_t->serverSignedData, \
			&s_csam_09_file_data[\
				S_CSAM_DRIVER_09FILE_SERVERSIGNEDDATA_INDEX], \
			S_CSAM_DRIVER_09FILE_SERVERSIGNEDDATA_SIZE);

	if(S_CSAM_DRIVER_OK != \
		S_CSAM_Driver_Get05FilePara(data_09file_t->terminalID, \
					S_CSAM_DRIVER_05FILE_TERMINALID_TYPE))
	{
		S_CSAM_DRIVER_FREE(data_09file_t);
		data_09file_t = NULL;
		S_CSAM_DRIVER_FREE(s_csam_09_file_data);
		s_csam_09_file_data = NULL;

		return NULL;
	}

	S_CSAM_DRIVER_FREE(s_csam_09_file_data);
		s_csam_09_file_data = NULL;

	return data_09file_t;
}

void S_CSAM_Driver_Get09File_StructPoint_Release(p_s_csam_driver_09file_t *p_data_09file_t)
{
	S_CSAM_DRIVER_FREE((*p_data_09file_t)->serverCCKSID);
	(*p_data_09file_t)->serverCCKSID = NULL;

	S_CSAM_DRIVER_FREE(*p_data_09file_t);
	*p_data_09file_t = NULL;
}
#endif

//#include "h_cc2640r2f_simpletime.h"
//
//typedef unsigned int time_unit;
//
//static time_unit s_csam_gettime(void)
//{
//    return H_CC2640R2F_SimpleTime_GetSystickms();
//}
//
//
//static void s_csam_driver_time_test(void)
//{
//    time_unit start_time = 0U;
//
//    s_csam_driver_rst_low();
//
//    while(1)
//    {
//        s_csam_driver_rst_high();
//        start_time = s_csam_gettime() + 10000;
//
//        while(start_time > s_csam_gettime())
//        {
//            continue;
//        }
//
//        s_csam_driver_rst_low();
//
//        start_time = s_csam_gettime() + 10000;
//
//        while(start_time > s_csam_gettime())
//        {
//            continue;
//        }
//    }
//
//}

void S_CSAM_Driver_ColdReset(void)
{

//    s_csam_driver_time_test();

	s_csam_driver_vcc_high();

	s_csam_driver_rst_low();

	s_csam_driver_delay_ms(10);

	s_csam_driver_rst_high();

}

signed char S_CSAM_Driver_Init(void)
{
	signed char rslt = 0;

	unsigned char setAID[2] = {0x3F, 0x01};
	unsigned char rdata[25];

	S_CSAM_Driver_ColdReset();

	rslt = s_csam_driver_getColdResetRsp();
//	S_CSAM_DRIVER_PRINT("PSAM cold rslt is %d\r\n", rslt);
	if(rslt)
	{
		return S_CSAM_DRIVER_COLDRESETERROR;
	}

	rslt = s_csam_driver_setAID(setAID, rdata);
//	S_CSAM_DRIVER_PRINT("PSAM set AID rslt is %d\r\n", rslt);
	if(rslt)
	{
		return S_CSAM_DRIVER_SETAIDERROR;
	}

//	unsigned char i = rdata[1];

//	printf("the len is %d\r\n", rdata[1]);

	rslt = S_CSAM_Driver_GetRsp(rdata, rdata[1]);

//	debug_hex(rdata, i);
////	S_CSAM_DRIVER_PRINT("PSAM get rsp rslt is %d\r\n", rslt);
	if(rslt)
	{
		return S_CSAM_DRIVER_GETRSPERROR;
	}

	// It is necessary to delay it
	H_CC2640R2F_SimpleTime_Around_Delay(100);


	return rslt;
}


//static const unsigned char s_csam_driver_getstandby_cmd[] = {0x00, 0xaa, 0x00, 0x00, 0x00, 0x04};
//
//signed char S_CSAM_Driver_Getstandby(void)
//{
//    signed char rslt = S_CSAM_DRIVER_OK;
//
//    s_csam_driver_senddata(s_csam_driver_getstandby_cmd, \
//                            sizeof(s_csam_driver_getstandby_cmd));
//
//#if S_CSAM_DRIVER_GETDATA_ECHO
//    rslt = s_csam_driver_getdata(sizeof(s_csam_driver_getstandby_cmd));
//#else
//    rslt = s_csam_driver_getdata(0);
//#endif
//
//    if(S_CSAM_DRIVER_OK == rslt)
//    {
//        //Check cmd rsp whether match
//        printf("the standy by is:");
//        debug_hex(s_csam_driver_rxbuffer, 10);
//        // printf("--rx len is %d\r\n", s_csam_driver_rxbufferlen);
////        if(s_csam_driver_rxbuffer[sizeof(s_csam_driver_getstandby_cmd)] \
////                == s_csam_driver_getstandby_cmd[1])
////        {
////            //Check the last two data
////            if(s_csam_driver_rxbuffer[\
////                S_CSAM_DRIVER_RANDOMDATA_LEN + \
////                S_CSAM_DRIVER_NORMALCMD_LEN - 1] == \
////                    0x90 && \
////                    s_csam_driver_rxbuffer[\
////                        S_CSAM_DRIVER_RANDOMDATA_LEN + \
////                        S_CSAM_DRIVER_NORMALCMD_LEN] == \
////                            0x00)
////            {
////                memcpy(random, \
////                       &s_csam_driver_rxbuffer[\
////                       S_CSAM_DRIVER_NORMALCMD_LEN + 1], \
////                        S_CSAM_DRIVER_RANDOMDATA_LEN);
////            }
////        // if(s_csam_driver_rxbuffer[0] == s_csam_driver_getrandom_cmd[1])
////        // {
////        //  //Check the last two data
////        //  if(s_csam_driver_rxbuffer[\
////        //      S_CSAM_DRIVER_RANDOMDATA_LEN - 1] == \
////        //          0x90 && \
////        //          s_csam_driver_rxbuffer[\
////        //              S_CSAM_DRIVER_RANDOMDATA_LEN] == \
////        //                  0x00)
////        //  {
////        //      memcpy(random, &s_csam_driver_rxbuffer[1], \
////        //              S_CSAM_DRIVER_RANDOMDATA_LEN);
////        //  }
////#else
////        if(s_csam_driver_rxbuffer[0] == s_csam_driver_getrandom_cmd[1])
////        {
////            //Check the last two data
////            if(s_csam_driver_rxbuffer[\
////                S_CSAM_DRIVER_RANDOMDATA_LEN - 1] == \
////                    0x90 && \
////                    s_csam_driver_rxbuffer[\
////                        S_CSAM_DRIVER_RANDOMDATA_LEN] == \
////                            0x00)
////            {
////                memcpy(random, &s_csam_driver_rxbuffer[1], \
////                        S_CSAM_DRIVER_RANDOMDATA_LEN);
////            }
////#endif
////            else
////            {
////                rslt = S_CSAM_DRIVER_RSPERROR;
////            }
////        }
////        else
////        {
////            printf("Get random S_CSAM_DRIVER_RSPNOTMATCH, it is 0x%02x and should be 0x%02x\r\n", \
////                s_csam_driver_rxbuffer[S_CSAM_DRIVER_NORMALCMD_LEN], \
////                s_csam_driver_getrandom_cmd[1]);
////            debug_hex(s_csam_driver_rxbuffer, s_csam_driver_rxbufferlen);
////            rslt = S_CSAM_DRIVER_RSPNOTMATCH;
////        }
//    }
//
//    // s_csam_driver_start_endrxdata();
//    return rslt;
//}

void s_csam_driver_halt(void)
{
    s_csam_driver_vcc_low();
    s_csam_driver_stop_rxdata();
}

void s_csam_driver_active(void)
{
    s_csam_driver_vcc_high();
}

//#include "s_lockcontrol.h"

void S_CSAM_Driver_Test(void)
{
	signed char rslt = 0;
//	unsigned char s_csam_mac_addr[6];

//	unsigned char lkey[S_CSAM_DATA_LKEY_LENGTH];
//	unsigned char passkey[S_CSAM_DATA_PASSKEY_LENGTH];

	rslt = S_CSAM_Driver_Init();
	S_CSAM_DRIVER_PRINT("The PSAM Init rslt is %d\r\n" , rslt);

//	if(0 == rslt)
//	{
//		if(0 == S_CSAM_Driver_Get05FilePara(s_csam_mac_addr, \
//					S_CSAM_DRIVER_05FILE_TERMINALID_TYPE))
//		{
//			 S_CSAM_DRIVER_PRINT("The terminal ID is: 0x");
//			 S_CSAM_DRIVER_PRINTHEX(s_csam_mac_addr, 6);
//
//			S_CSAM_Data_Update_LockID(s_csam_mac_addr);
//			S_CSAM_Data_Update_BLEAddr(s_csam_mac_addr);
//
//
//			s_csam_driver_init_rslt_flag = csam_init_success;
//		}
//		else
//		{
//			S_CSAM_DRIVER_PRINT("Get terminal ID error\r\n");
//			s_csam_driver_init_rslt_flag = csam_init_terminalID_failed;
//		}
//
//		if(0 == S_CSAM_Driver_Get08FilePara(lkey, S_CSAM_DRIVER_08FILE_LKEY_TYPE))
//		{
//		    S_CSAM_Data_Updata_LKey(lkey);
//		}
//		else
//		{
//            S_CSAM_DRIVER_PRINT("Get lkey error\r\n");
//            s_csam_driver_init_rslt_flag = csam_init_terminalID_failed;
//		}
//
//		if(0 == S_CSAM_Driver_Get08FilePara(passkey, S_CSAM_DRIVER_08FILE_PASSKEYPRIVILEGEDTIME_TYPE))
//        {
//		    S_CSAM_Data_Updata_PasskeyPrivilegedTime(passkey);
//        }
//        else
//        {
//            S_CSAM_DRIVER_PRINT("Get passkeyprivilegedtime error\r\n");
//            s_csam_driver_init_rslt_flag = csam_init_terminalID_failed;
//        }
//
//		if(0 == S_CSAM_Driver_Get08FilePara(passkey, S_CSAM_DRIVER_08FILE_PASSKEYEFFECTIVELIFE_TYPE))
//        {
//		    S_CSAM_Data_Updata_PasskeyEffectiveLife(passkey);
//        }
//        else
//        {
//            S_CSAM_DRIVER_PRINT("Get passkeyeffectivelife error\r\n");
//            s_csam_driver_init_rslt_flag = csam_init_terminalID_failed;
//        }
//
//		S_LockControl_Ask2Success();
//
//	}
//	else
//	{
//	    S_LockControl_Ask2Failed();
//	}
//
////	S_CSAM_Driver_Getstandby();
//
	s_csam_driver_vcc_low();
	s_csam_driver_stop_rxdata();

}
